package cn.iocoder.yudao.module.moa.controller.admin.qjapplysub;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.moa.controller.admin.qjapplysub.vo.*;
import cn.iocoder.yudao.module.moa.dal.dataobject.qjapplysub.QjApplySubDO;
import cn.iocoder.yudao.module.moa.service.qjapplysub.QjApplySubService;

@Tag(name = "管理后台 - MOA 请假关联")
@RestController
@RequestMapping("/moa/qj-apply-sub")
@Validated
public class QjApplySubController {

    @Resource
    private QjApplySubService qjApplySubService;

    @PostMapping("/create")
    @Operation(summary = "创建MOA 请假关联")
    public CommonResult<Long> createQjApplySub(@Valid @RequestBody QjApplySubSaveReqVO createReqVO) {
        return success(qjApplySubService.createQjApplySub(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新MOA 请假关联")
    public CommonResult<Boolean> updateQjApplySub(@Valid @RequestBody QjApplySubSaveReqVO updateReqVO) {
        qjApplySubService.updateQjApplySub(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除MOA 请假关联")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteQjApplySub(@RequestParam("id") Long id) {
        qjApplySubService.deleteQjApplySub(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得MOA 请假关联")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<QjApplySubRespVO> getQjApplySub(@RequestParam("id") Long id) {
        QjApplySubDO qjApplySub = qjApplySubService.getQjApplySub(id);
        return success(BeanUtils.toBean(qjApplySub, QjApplySubRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得MOA 请假关联分页")
    public CommonResult<PageResult<QjApplySubRespVO>> getQjApplySubPage(@Valid QjApplySubPageReqVO pageReqVO) {
        PageResult<QjApplySubDO> pageResult = qjApplySubService.getQjApplySubPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, QjApplySubRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出MOA 请假关联 Excel")
    @ApiAccessLog(operateType = EXPORT)
    public void exportQjApplySubExcel(@Valid QjApplySubPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<QjApplySubDO> list = qjApplySubService.getQjApplySubPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "MOA 请假关联.xls", "数据", QjApplySubRespVO.class,
                        BeanUtils.toBean(list, QjApplySubRespVO.class));
    }

}
